'''
Created on Jul 17, 2012

@author: daoxuandung
'''
from web.base import BaseHandler
from web import urls, settings
import json
import logging
from google.appengine.ext import ndb
from entity.entity import Article, Board
from app import article_stat
from webapp2_extras.appengine.auth.models import User


class UpdateStatHandler(BaseHandler):
    @ndb.synctasklet
    def post(self):
        # Get article
        article_id = self.request.params.get('article')
        article_key = ndb.Key(Article, int(article_id))
        
        # Save vote to datastore
        yield article_stat.save_stats_async(article_key)


class UserSetupHandler(BaseHandler):
    # Setup data for first time user 
    # including default bookmark list
    def post(self):
        user_id = self.request.params.get('user_id')
        logging.info("user_id: " + user_id)
        user_key = ndb.Key(User, int(user_id))
        qry = Board.query(Board.name==settings.DEFAULT_BOOKMARK,
                                Board.user==user_key)
        board = qry.get()
        if not board:
            board = Board()
            board.name = settings.DEFAULT_BOOKMARK
            board.user = user_key
            board.put()
    
app = urls.app

def main():
    app.run()

if __name__ == '__main__':
    main()